<?
/*
 * Created on Jan 27, 2008
 *
 * This is an open source software, please obey the GPL
 * The Referee Service is offered by Cacomand Inc. 
 * View Http://www.cacomand.com for support or more information
 * You can also view distrema.googlecode.com to join the community.
 */
include("lib_dps.php");
class witkey_user extends dps_user
{
	/**
	* The graduate school of the user
	*
	* @var       string
	* @access    public
	*/
	public $graduate_school;
	/**
	* The major of the user
	*
	* @var       string
	* @access    public
	*/
	public $major;
	/******************************************************************************
  * Constructor of the witkey user class
  *
  * Return unavailable.
  *
  * usage: witkey_user(string $epsn, integer $user_ID)
  *
  * @access    public
  * @return    boolean
  */
	function witkey_user($epsn,$user_ID)
	{
		parent::user($epsn,$user_ID);
		$this->graduate_school="未登记";
		$this->major="未登记";
	}
	
	/******************************************************************************
  * Get the certificate of the user, and fill the $graduate_school and $major
  *
  * Returns true on success, false on error or not found.
  *
  * usage: gain_certification()
  *
  * @access		public
  * @return		boolean
  * @see		$graduate_school
  * @see		$major
  */
	function gain_certification()
	{
		$this->gain_user_level();
		if($this->user_level==2)
		{
			global $ref_link;
			if ($ref_link!=FALSE)
			{
				$select_format='SELECT school,major FROM diploma_pool WHERE epsn="%s" AND user_ID=%d';
				$query=sprintf($select_format,$this->epsn,$this->user_ID);
				if($developer)
					echo("".$query."<br />");
				$res_dip=mysql_query($query,$ref_link);
				if($obj_dip=mysql_fetch_object($res_dip))
				{
					$this->graduate_school=$obj_dip->school;
					$this->major=$obj_dip->major;
					return true;
				}
				else
				{
					return false;
				}
			}
		}
		else
		{
			return false;
		}
	}
}	
/**
 * A class for basic witkey actions
 *
 * @version		0.5.35
 * @author		Arthur<arthur@cacomand.com>
 * @package		DPS
 *
 */
class witkey
{
	/******************************************************************************
  * To bid for a task
  *
  * Returns true on success, false on error.
  *
  * usage: bid_task(task $task,witkey_user $service_provider,integer $charge,integer $finish_time,string $memo)
  *
  * @access		public
  * @return		boolean
  */
	function bid_task($task,$service_provider,$charge,$finish_time,$memo)
	{
		$bid_full_name=$task->task_full_name.".".$service_provider->epsn.".".$service_provider->user_ID;
		if(send_data($service_provider,$task->service_buyer,4,$task->task_full_name,$service_provider->epsn.".".$service_provider->user_ID) AND send_data($service_provider,$task->service_buyer,5,$bid_full_name,$charge) AND send_data($service_provider,$task->service_buyer,6,$bid_full_name,$finish_time) AND send_data($service_provider,$task->service_buyer,8,$bid_full_name,$memo))
			return true;
		else
			return false;
	}
	
/******************************************************************************
  * Publish new task onto the DB
  *
  * Returns true on success, false on error.
  *
  * usage: new_task(witkey_user $service_buyer,integer $price,integer $deadline,string $memo)
  *
  * @access		public
  * @param		$service_buyer		the publisher
  * @param		$task_full_name		the category and name of the task
  * @param		$price				the price which the publiser think is resonable
  * @param		$deadline			the time the publisher think is enough,0 for absolute
  * $param		$memo				introduction if the task
  * @return		boolean
  */
	function new_task($service_buyer,$task_full_name,$price,$deadline,$memo)
	{
		$service_buyer->add_record(1,$task_full_name,"open");
		$service_buyer->add_record(2,$task_full_name,$price);
		$service_buyer->add_record(3,$task_full_name,$deadline);
		$service_buyer->add_record(7,$task_full_name,$memo);
	}
	
	/******************************************************************************
  * Get a list of the tasks
  *
  * Returns result in an array on success, false on error.
  *
  * usage: get_tasks([string $category])
  *
  * @access		public
  * @param		string $category	filter for the prefix
  * @return		array
  */
	function get_tasks()
	{
		if(func_num_args()==1)
			$category=func_get_arg(0);
		else
			$category="task";
		$return_array=get_searched_data(1,$category);
		return $return_array;
	}
}
/**
 * A class for tasks
 *
 * @version		0.5.35
 * @author		Arthur<arthur@cacomand.com>
 * @package		DPS
 *
 */
class task
{
	/**
	* Full name of the task(with category)
	*
	* @var			string
	* @access		public
	*/
	public $task_full_name;
	/**
	* Price given by the service buyer
	*
	* @var			integer
	* @access		public
	*/
	public $price;
	/**
	* The time the service buyer think is acceptable
	*
	* @var			integer
	* @access		public
	*/
	public $deadline;
	/**
	* An array for bids of the task
	*
	* @var			array
	* @access		public
	*/
	public $bids;
	/**
	* The buyer this task belongs to
	*
	* @var			witkey_user
	* @access		public
	*/
	public $service_buyer;
	/******************************************************************************
  * Constructor of the task class,search the DB for it's info and bids
  *
  * Return unavailable
  *
  * usage: task(string $task_full_name)
  *
  * @access		public
  * @param		string $task_full_name	Identifer to locate the task
  * @return		N/A
  */
	function task($task_full_name)
	{
		//TODO:使用任务全名找出任务的相关信息，并填写相关变量;
	}
	/******************************************************************************
  * The owner choose the user to win the bid
  *
  * Returns true on success, false on error.
  *
  * usage: choose_bider(object $service_provider)
  *
  * @access		public
  * @return		boolean
  */
	function choose_bider($service_provider)
	{
		$this->service_buyer->reset_record(1,$this->task_full_name,$service_provider->epsn.".".$service_provider->user_ID);
		$this->service_buyer->del_record(4,$this->task_full_name);
		$this->service_buyer->del_record(5,$this->task_full_name);
		$this->service_buyer->del_record(6,$this->task_full_name);
		$this->service_buyer->del_record(8,$this->task_full_name);
	}
	/******************************************************************************
  * The owner close the task and give the 70% money of the task to all the biders
  *
  * Returns true on success, false on error.
  *
  * usage: close_task()
  *
  * @access		public
  * @return		boolean
  */
	function close_task()
	{
		$this->service_buyer->reset_record(1,$this->task_full_name,"abandoned");
		//TODO:把75%的钱分发给投标者,把剩余25%的钱解冻
		$this->service_buyer->del_record(4,$this->task_full_name);
		$this->service_buyer->del_record(5,$this->task_full_name);
		$this->service_buyer->del_record(6,$this->task_full_name);
		$this->service_buyer->del_record(8,$this->task_full_name);
		return true;
	}
}

/**
 * a class for bids
 *
 * @version		0.5.35
 * @author		Arthur<arthur@cacomand.com>
 * @package		DPS
 *
 */
class bid
{
	public $charge;
	public $finish_time;
	public $memo;
	public $bid_full_name;
	public $task_belong;
	public $bidder;
	function bid($task_belong,$bidder)
	{
		$this->task_belong=$task_belong;
		$this->bidder=$bidder;
		$this->bid_full_name=$this->task_belong->task_full_name.".".$bidder->epsn.".".$bidder->user_ID;
		if(	$charge_array=$this->task_belong->service_buyer->smart_get(false,5,$this->bid_full_name) AND 
			$finish_time_array=$this->task_belong->service_buyer->smart_get(false,6,$this->bid_full_name) AND 
			$memo_array=$this->task_belong->service_buyer->smart_get(false,8,$this->bid_full_name))
		{
			$this->charge=$charge_array[0]["memo"];
			$this->finish_time=$finish_time_array[0]["memo"];
			$this->memo=$memo_array[0]["memo"];
			return true;
		}
		else
			return false;
	}
}
?>
